package com.jiang.smartcommunity.ui.activity;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.constraintlayout.utils.widget.ImageFilterView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.jiang.smartcommunity.R;
import com.jiang.smartcommunity.tool.Tool;

import java.util.List;

public class S3Activity extends BaseActivity implements View.OnClickListener {

    private RecyclerView rv;
    private FloatingActionButton floatingActionButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_s3);
        initView();
        setTitle("友邻社交");
    }

    private void initView() {
        rv = (RecyclerView) findViewById(R.id.rv);
        floatingActionButton = (FloatingActionButton) findViewById(R.id.floatingActionButton);

        floatingActionButton.setOnClickListener(this);
        rv.setLayoutManager(new LinearLayoutManager(getThis()));


    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshUI();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        System.gc();
    }

    private void refreshUI() {
        int lay = R.layout.item_s3;
        rv.setAdapter(new RecyclerView.Adapter<ViewHolder>() {
            @NonNull
            @Override
            public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new ViewHolder(LayoutInflater.from(getThis()).inflate(lay, parent, false));
            }

            @Override
            public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
                Bean bean = Tool.beans.get(position);
                if (bean.bitmap != null) {
                    holder.imageView3.setImageBitmap(bean.bitmap);
                } else {
                    holder.imageView3.setImageResource(bean.res);
                }
                holder.imageView7.setImageResource(bean.ava);
                holder.tv_name.setText(bean.name);
                holder.textView3.setText(bean.c);
                holder.cv.setOnClickListener(v -> {
                    Tool.carIndex = position;
                    startActivity(new Intent(getThis(), PicActivity.class));
                });
            }

            @Override
            public int getItemCount() {
                return Tool.beans.size();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.floatingActionButton:
                startActivity(new Intent(getThis(), S3DActivity.class));
                break;
        }
    }

    public static class Bean {
        String name, c;
        int res, ava;
        Bitmap bitmap;
        List<LiuYan> liuYans;

        public Bean(String name, String c, int res, int ava, Bitmap bitmap, List<LiuYan> liuYans) {
            this.name = name;
            this.c = c;
            this.res = res;
            this.ava = ava;
            this.bitmap = bitmap;
            this.liuYans = liuYans;
        }

        public static class LiuYan {
            String c, name;
            int ava;

            public LiuYan(String c, String name, int ava) {
                this.c = c;
                this.name = name;
                this.ava = ava;
            }
        }
    }

    public static
    class ViewHolder extends RecyclerView.ViewHolder {
        public View rootView;
        public ImageFilterView imageView7;
        public ImageView imageView3;
        public CardView cv;
        public TextView textView3;
        public TextView tv_name;


        public ViewHolder(View rootView) {
            super(rootView);
            this.rootView = rootView;
            this.imageView7 = (ImageFilterView) rootView.findViewById(R.id.imageView7);
            this.imageView3 = (ImageView) rootView.findViewById(R.id.imageView3);
            this.cv = (CardView) rootView.findViewById(R.id.cv);
            this.textView3 = (TextView) rootView.findViewById(R.id.textView3);
            this.tv_name = (TextView) rootView.findViewById(R.id.tv_name);
        }

    }
}